package com.sdy.healthcode.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.dto.ThisCourtVO;
import com.sdy.healthcode.biz.mapper.UserMapper;
import com.sdy.healthcode.biz.model.User;
import com.sdy.healthcode.biz.service.UserService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zzq
 * @since 2020-02-18
 */
@Slf4j
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByPhone(String phone) {
        return lambdaQuery().eq(User::getPhone, phone).one();
    }

    @Override
    public User getUserByWxid(String wxid) {
        return lambdaQuery().eq(User::getWxid, wxid).one();
    }

    @Override
    public void updateUserWxid(Integer userId, String wxid) {
        lambdaUpdate().eq(User::getWxid, wxid).ne(User::getId, userId).set(User::getWxid, null).update();
        lambdaUpdate().eq(User::getId, userId).set(User::getWxid, wxid).update();
    }

    @Override
    public User createUser(String phone, String wxid) {
        User user = new User()
                .setPhone(phone)
                .setAuthScan(0)
                .setAuthManage(0)
                .setType(1)
                .setWxid(wxid)
                .setCreateTime(new Date())
                .setAddrLocal("");
        save(user);
        return user;
    }

    @Override
    public User xGetOrCreateUserByPhone(String phone, String wxid) {
        User user = getUserByPhone(phone);
        if (user == null) {
            user = createUser(phone, wxid);
        } else if (StringUtil.isNotBlank(wxid)) {
            updateUserWxid(user.getId(), wxid);
        }
        return user;
    }

    @Override
    public User xGetOrCreateUserByWxid(String wxid) {
        User user = getUserByWxid(wxid);
        if (user == null) {
            user = createUser(null, wxid);
        }
        return user;
    }

    @Override
    public Boolean checkPriviledgeOfRecord(Integer recorderId, Integer userId) {
        User recorder = getById(recorderId);
        return recorder != null && recorder.getAuthScan().equals(1);
    }

    @Override
    public void updateUserInfo(Integer userId, Integer cardType, String cardId, String addr, String addrLocal, String phone, String name) {
        lambdaUpdate()
                .eq(User::getId, userId)
                .set(StringUtil.isNotBlank(name), User::getName, name)
                .set(cardType != null, User::getCardType, cardType)
                .set(StringUtil.isNotBlank(cardId), User::getCardId, cardId)
                .set(StringUtil.isNotBlank(addr), User::getAddr, addr)
                .set(StringUtil.isNotBlank(addrLocal), User::getAddrLocal, addrLocal)
                .set(StringUtil.isNotBlank(phone), User::getPhone, phone)
                .update();
    }

    @Override
    public IPage<ThisCourtVO> getPage(String name, Integer type, Integer authScan, Integer authCockpit, Page<ThisCourtVO> page) {
        return userMapper.getPage(page, name, type, authScan, authCockpit);
    }

    @Override
    public void updAuthScan(Integer id, Integer type) {
        lambdaUpdate()
                .eq(User::getId,id)
                .set(User::getAuthScan, type)
                .update();
    }

    @Override
    public void updAuthCockpit(Integer id, Integer type) {
        lambdaUpdate()
                .eq(User::getId,id)
                .set(User::getAuthCockpit, type)
                .update();
    }
}
